package com.njtoyo.taxi.admin.manager.factory;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.CallEndSend;
import com.njtoyo.taxi.admin.mapper.master.mapper.CallEndSendMapper;
import com.njtoyo.taxi.admin.utils.DateUtils;
import com.njtoyo.taxi.admin.utils.OssClient;

import com.njtoyo.taxi.admin.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 * 
 * @author qiup
 */
@Slf4j
public class AsyncFactory
{
    private static final Logger asyncTask = LoggerFactory.getLogger("asyncTask");


    /**
     * 转存东信通话录音文件
     *
     * @return 任务task
     */
    public static TimerTask saveToCloud(final String dxUrl,String callid)
    {

        log.info("start save,dxurl:{},callid:{}",dxUrl,callid);
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String tempDir="/downloadFile/temp/dx/"+ DateUtils.getDate()+"/";
                //先下载到本地
                String fileName=dxUrl.substring(dxUrl.lastIndexOf("/") + 1);
                String localFullPath=tempDir+fileName;
                File file=HttpUtil.downloadFileFromUrl(dxUrl,new File(localFullPath));
                        //DownloadUtil.download(dxUrl,tempDir);
                log.info("download success,name:{}",file.getName());
                try {
                    InputStream in = new FileInputStream(file);
                    //转存到天翼云
                    String ossUrl= OssClient.uploadFile(in,file.getName(),OssClient.OOS_BUCKET_NAME_CALLRECORD);
                    log.info("upload to oss success,url:{}",ossUrl);
                    //根据callid更新表
                    CallEndSend callEndSend=new CallEndSend();
                    callEndSend.setRecordUrlOss(ossUrl);
                    UpdateWrapper<CallEndSend> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("callid", callid);
                    int result =SpringUtils.getBean(CallEndSendMapper.class).update(callEndSend,updateWrapper);
                    if(result>0){
                        log.info("update mysql success,callid:{}",callid);
                            if(file.delete()){
                                log.info("delete temp file success,local path:{}",localFullPath);
                            }else{
                                log.info("delete temp file fail,local path:{}",localFullPath);
                            }
                    }
                } catch (FileNotFoundException e) {
                    log.error(e.getMessage());
                }catch (IOException e){
                    log.error("delete error,local path:{},error:{}",localFullPath,e.getMessage());
                }

            }
        };
    }
    public static void main(String[] args){
        String fileName = "http://61.182.139.17:9101/spnscp/core/record/download/CALL/SLST_F_SCP/CHB2407171324590A21184211576798.mp3";
        String fileExtension = fileName.substring(fileName.lastIndexOf("/") + 1);
        System.out.println(fileExtension); // 输出: txt
    }


}
